package com.zang.linkedlist;

/**
 * @author ZVerify
 * @since 2022/10/11 10:17
 **/
public class 删除链表的倒数第N个节点 {

    public ListNode removeNthFromEnd(ListNode head, int n) {

        if (head==null) return null;

        if (head.next == null && n == 1) return null;

        ListNode node = new ListNode(-1);

        node.next = head;

        ListNode cur = node;
        ListNode pre = node;


        int index = 0;
        while (index!=n){
            cur = cur.next;
            index++;
        }

        while (cur.next!=null){
            cur = cur.next;
            pre = pre.next;
        }

        pre.next = pre.next.next;

        return node.next;




    }
}
